var fs = require("fs");
var path = require("path");
var query = require("../db/db");


exports.datactrl = {
    jsonback: {
        success: '',
        msg: '',
        data: ''
    },
    day: function (req, callback) {
        var that = this;
        var gamekind = req.query.gamekind;
        var datecome = req.query.date;
        var page = req.query.page;
        var nums = req.query.nums;
        if (!gamekind) {
            that.jsonback.success = false;
            that.jsonback.msg = '未指gamekind';
            that.jsonback.data = '';
            that.jsonback.total = 0;
            callback(that.jsonback);
            return
        }
        if (!datecome) {
            that.jsonback.success = false;
            that.jsonback.msg = '未指date';
            that.jsonback.data = '';
            that.jsonback.total = 0;
            callback(that.jsonback);
            return
        }
        if (datecome.length == 8) {
            var year = datecome.slice(0, 4);
            var month = datecome.slice(4, 6);
            var day = datecome.slice(6, 8);
            var date = year + '-' + month + '-' + day;
            if (!nums) {
                nums = 10;
            }
            if (!page) {
                page = 1;
            }
            var sql1;
            if (page > 1) {
                sql1 = 'select * from result where daystr = "' + date + '" and gamekind = "' + gamekind + '" order by gameid desc limit ' + (page - 1) * nums + ',' + nums + ';';
            } else if (page == 1) {
                sql1 = 'select * from result where daystr = "' + date + '" and gamekind = "' + gamekind + '" order by gameid desc limit 0,' + nums + ';';
            } else {
                sql1 = 'select * from result where daystr = "' + date + '" and gamekind = "' + gamekind + '" order by gameid desc;'
            }
            console.log(sql1)

            query(sql1, function (err, vals, fields) {
                if (err) {
                    that.jsonback.success = false;
                    that.jsonback.msg = '查询时出错';
                    that.jsonback.data = '';
                    that.jsonback.total = 0;
                    callback(that.jsonback);
                    throw err
                }
                if (vals.length) {
                    that.jsonback.success = true;
                    that.jsonback.msg = '查询成功';
                    that.jsonback.data = vals;
                    that.jsonback.total = vals.length;
                    callback(that.jsonback);
                } else {
                    that.jsonback.success = false;
                    that.jsonback.msg = '如参数正确,该日期无数据';
                    that.jsonback.data = '';
                    that.jsonback.total = 0;
                    callback(that.jsonback);
                }
            })
        } else {
            that.jsonback.success = false;
            that.jsonback.msg = '日期参数错误';
            that.jsonback.data = '';
            that.jsonback.total = 0;
            callback(that.jsonback);
        }
    },
    id: function (req, callback) {
        var that = this;
        var gameid = req.query.gameid;
        var gamekind = req.query.gamekind;
        if (!gameid) {
            that.jsonback.success = false;
            that.jsonback.msg = '未指定gameid';
            that.jsonback.data = '';
            that.jsonback.total = 0;
            callback(that.jsonback);
            return
        }
        if (!gamekind) {
            that.jsonback.success = false;
            that.jsonback.msg = '未指gamekind';
            that.jsonback.data = '';
            that.jsonback.total = 0;
            callback(that.jsonback);
            return
        }
        var sql1 = 'select * from result where gameid ="' + gameid + '" and gamekind = "' + gamekind + '";';
        query(sql1, function (err, vals, fields) {
            if (err) {
                that.jsonback.success = false;
                that.jsonback.msg = '查询时出错';
                that.jsonback.data = '';
                that.jsonback.total = 0;
                callback(that.jsonback);
                throw err
            }
            if (vals.length) {
                that.jsonback.success = true;
                that.jsonback.msg = '查询成功';
                that.jsonback.data = vals;
                that.jsonback.total = vals.length;
                callback(that.jsonback);
            } else {
                that.jsonback.success = false;
                that.jsonback.msg = '如参数正确,该期无数据';
                that.jsonback.data = '';
                that.jsonback.total = 0;
                callback(that.jsonback);
            }
        })
    },
    last10: function (req, callback) {
        var that = this;
        var gamekind = req.query.gamekind;
        var sql1 = 'select * from result where gamekind = "' + gamekind + '" order by gameid desc limit 0,10;';
        query(sql1, function (err, vals) {
            if (err) {
                that.jsonback.success = false;
                that.jsonback.msg = '查询时出错';
                that.jsonback.data = '';
                that.jsonback.total = 0;
                callback(that.jsonback);
                throw err
            }
            if (vals.length) {
                that.jsonback.success = true;
                that.jsonback.msg = '查询成功';
                that.jsonback.data = vals;
                that.jsonback.total = vals.length;
                callback(that.jsonback);
            } else {
                that.jsonback.success = false;
                that.jsonback.msg = '数据为空';
                that.jsonback.data = '';
                that.jsonback.total = 0;
                callback(that.jsonback);
                throw err
            }
        })
    }
};